//
//  NewsQuestionCell.m
//  suRead
//
//  Created by nancy  on 16/9/12.
//  Copyright © 2016年 bao. All rights reserved.
//

#import "NewsQuestionCell.h"

#import "M_News.h"

#import "UIImageView+WebCache.h"
#import "UIControl+Blocks.h"
#import <Masonry/Masonry.h>
#import "AccountManager.h"
#import "NewCellManager.h"

@interface NewsQuestionCell()
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelTitle;
@property (weak, nonatomic) IBOutlet UILabel *labelCreateTime;

@property (weak, nonatomic) IBOutlet UILabel *labelViewCount;
/**
 *  @brief 赞lable
 */
@property (strong, nonatomic) IBOutlet UILabel *zanLable;
@property (weak, nonatomic) IBOutlet UIImageView *zanIcon;
@property (weak, nonatomic) IBOutlet UIImageView *viewIcon;
@property (weak, nonatomic) IBOutlet UIImageView *zhuanjiaIcon;
@property (weak, nonatomic) IBOutlet UILabel *zhuanjia_nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *zhuanjia_descriptionLabel;
@property (weak, nonatomic) IBOutlet UIView *categoryView;

@property (weak, nonatomic) IBOutlet UIView *viewCountContainer;
@property (weak, nonatomic) IBOutlet UIImageView *imageViewFlag;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *flagWidthConstraint;

@end

@implementation NewsQuestionCell

- (void)setFrame:(CGRect)frame {
    frame.origin.y += 5;
    frame.size.height -= 5;
    [super setFrame:frame];
}

- (void)awakeFromNib {
    // Initialization code
    [super awakeFromNib];
    if ( [[[UIDevice currentDevice]systemVersion] floatValue] >= 8.0) {
        self.layoutMargins = UIEdgeInsetsZero;
        self.preservesSuperviewLayoutMargins = NO;
        self.separatorInset = UIEdgeInsetsMake(0, 10.0f, 0, 10.0f);
    }
}

- (void)setNewsData:(id)newsData
{
    M_News *news = (M_News *)newsData;
    
    NSString *imageURL = news.f_background_img;
    [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:imageURL] placeholderImage:[UIImage imageNamed:@"lazy"]];
    
    NSString *title = news.f_title;
    self.labelTitle.text = title;
    
    //已读未读
    if ([[NewCellManager shared] hasRead:news.f_id]) {
        self.labelTitle.textColor = [UIColor lightGrayColor];
    } else {
        self.labelTitle.textColor = [UIColor blackColor];
    }
    
    NSString *createTime = news.f_updateTime;
    self.labelCreateTime.text = createTime;
    
    self.zhuanjiaIcon.layer.cornerRadius  = 20.0;//设置图片圆角的尺度
    self.zhuanjiaIcon.layer.masksToBounds = YES;//没这句话它圆不起来
    //    self.userPhoto.image = [UIImage imageNamed:@"icon_user"];
    [self.zhuanjiaIcon sd_setImageWithURL:[NSURL URLWithString:news.f_zhuanjia_img] placeholderImage:[UIImage imageNamed:@"icon_user"]];
    self.zhuanjia_nameLabel.text=news.f_zhuanjia_name;
    self.zhuanjia_descriptionLabel.text=news.f_zhuanjia_touxian;
    
    NSArray  *categorylist=news.f_category;
    
    CGFloat tagDistance = 10.0f;
    CGFloat tagPadding  = 5.0f;
    CGFloat tagHeight   = 18.0f;
    UIFont  *tagFont    = [UIFont systemFontOfSize:12.0f];
    
    CGSize screenSize = [UIScreen mainScreen].bounds.size;
    
    __block CGFloat currentX =screenSize.width-20;
    __block CGFloat currentY = 3;
    [self.categoryView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //逆向遍历
    NSEnumerator *enumerator = [categorylist reverseObjectEnumerator];
    categorylist = (NSArray*)[enumerator allObjects];
    
    for (NSDictionary *itemDic in categorylist){
        
        NSString *categoryName = [itemDic objectForKey:@"name"];
        
        NSString *categoryurl=[itemDic objectForKey:@"url"];
        
        CGSize size = [categoryName sizeWithAttributes:@{NSFontAttributeName: tagFont}];
        
        UIButton *ctgButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [ctgButton addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
            //去问吧专题列表
            if ([_newsQuestionCellDelegate respondsToSelector:@selector(toWenBaCtgListPage:)]) { // 如果协议响应了sendValue:方法
                [_newsQuestionCellDelegate toWenBaCtgListPage:categoryurl]; // 通知执行协议方法
            }
            
        }];
        [ctgButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [ctgButton setTitle:categoryName forState:UIControlStateNormal];
        ctgButton.layer.borderWidth = 0.5f;
        ctgButton.layer.borderColor = [UIColor whiteColor].CGColor;
        ctgButton.layer.cornerRadius=5.0f;
        ctgButton.layer.masksToBounds = YES;
        ctgButton.titleLabel.font = tagFont;
        [self.categoryView addSubview:ctgButton];
        
        [ctgButton mas_makeConstraints:^(MASConstraintMaker *make) {
            
            //先判断当前行是否放的下
            currentX = (currentX -tagPadding * 2 -size.width-tagDistance);
            make.left.equalTo(ctgButton.superview).offset(currentX);
            make.top.equalTo(ctgButton.superview).offset(currentY);
            make.size.mas_equalTo(CGSizeMake(tagPadding * 2 + size.width, tagHeight));
            
        }];
        
    }
    
    if ([AccountManager sharedAccountManager].f_isShowViewCount) {
        self.viewCountContainer.hidden = NO;
        self.labelViewCount.text = news.f_views;
        [self.zanLable setText:[NSString stringWithFormat:@"%ld",news.f_square]];
    } else {
        self.viewCountContainer.hidden = YES;
    }
    
    /*
     置顶|99
     最新|1
     实发|2
     热点|3
     独家|4
     专题|5
     推广|6
     视频|7
     直播|8
     福利9
     */
    self.flagWidthConstraint.constant = 36.0f;
    NSString *flagName = @"";
    switch ([news.f_newsFlag intValue]) {
        case 99:
            flagName = @"news_flag_01";
            break;
        case 1:
            flagName = @"news_flag_02";
            break;
        case 2:
            flagName = @"news_flag_03";
            break;
        case 3:
            flagName = @"news_flag_04";
            break;
        case 4:
            flagName = @"news_flag_05";
            break;
        case 5:
            flagName = @"news_flag_06";
            break;
        case 6:
            flagName = @"news_flag_07";
            break;
        case 7:
            flagName = @"news_flag_08";
            break;
        case 8:
            flagName = @"news_flag_09";
            break;
        case 9:
            flagName = @"news_flag_10";
            break;
        default:
            flagName = @"icon_time";
            self.flagWidthConstraint.constant = 15.0f;
            break;
    }
    self.imageViewFlag.image = [UIImage imageNamed:flagName];
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
